#include"datebase.h"
#include <mysql/field_types.h>
#include <mysql/mysql.h>
#include <stdint.h>
#include <stdlib.h>
#include "user_check.h"

int datebase_conect(MYSQL* pconn,MYSQL_STMT** stmt){
    printf("datebase_conect\n");
    pconn=mysql_init(pconn);
    MYSQL* p=mysql_real_connect(pconn,
                                "sqleast.mysql.database.azure.com",
                                "zefan",
                                "1234lxlLXL",
                                "sudo rm -rf",
                                0,
                                NULL,
                                0);
    if(p==NULL){
        printf("%s\n",mysql_error(pconn));
        return -1;
    }

    *stmt = mysql_stmt_init(pconn);
    if(stmt==NULL){
        printf("(%d,%s)\n",mysql_errno(pconn),mysql_error(pconn));
        mysql_close(pconn);
        return -1;
    }
    return 1;
}   

int prepare_count(MYSQL_STMT* stmt,MYSQL* pconn,const char* sql){

    int ret=mysql_stmt_prepare(stmt,sql,strlen(sql));
    if(ret){
        printf("(%d,%s)\n",mysql_stmt_errno(stmt),mysql_stmt_error(stmt));                           
        mysql_close(pconn);
        return -1;
    }

    int count=mysql_stmt_param_count(stmt);


    return count;
}
bool select_username(const char* username,MYSQL_STMT* stmt,MYSQL pconn)
{
    printf("select_username\n");

    char* sql="SELECT username FROM user_table WHERE username = ?";
    int count=prepare_count(stmt,&pconn,sql);

    char userName[20]={0};
    unsigned long Name_len=0;
    MYSQL_BIND bind;
    memset(&bind,0,sizeof(bind));

    bind.buffer_type=MYSQL_TYPE_VAR_STRING;
    bind.buffer=userName;
    bind.is_null=0;
    bind.length=&Name_len;

    int ret = mysql_stmt_bind_param(stmt, &bind);
    if(ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(stmt),
               mysql_stmt_error(stmt));
        mysql_close(&pconn);
        return false;
    }

    strcpy(userName,username);
    Name_len=strlen(userName);
    ret=mysql_stmt_execute(stmt);
    if(ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(stmt),
               mysql_stmt_error(stmt));
        mysql_close(&pconn);
        return false;
    }

 int i=mysql_stmt_fetch(stmt);
    if(i==1||i==MYSQL_NO_DATA){
        return true;
    }else
    {
        return false;
    }

}
bool insert_username(user_info_t* user,MYSQL_STMT* stmt,MYSQL pconn){
    printf("insert_username\n");

    char* sql="INSERT INTO user_table(username,cryptpasswd,pwd) VALUES(?,?,?)";
    int count=prepare_count(stmt,&pconn,sql);

    char Name[20]={0};
    unsigned long Name_len=0;
    
    char Encrypted[150]={0};
    unsigned long Encrypted_len=0;

    int Pwd;
    unsigned long Pwd_len=0;

    MYSQL_BIND bind[3];
    memset(&bind,0,sizeof(bind));

    bind[0].buffer_type=MYSQL_TYPE_VAR_STRING;
    bind[0].buffer=Name;
    bind[0].is_null=0;
    bind[0].length=&Name_len;

    bind[1].buffer_type=MYSQL_TYPE_VAR_STRING;
    bind[1].buffer=Encrypted;
    bind[1].is_null=0;
    bind[1].length=&Encrypted_len;

    bind[2].buffer_type=MYSQL_TYPE_LONG;
    bind[2].buffer=&Pwd;
    bind[2].is_null=0;
    bind[2].length=&Pwd_len;
    int ret = mysql_stmt_bind_param(stmt, bind);
    if(ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(stmt),
               mysql_stmt_error(stmt));
        mysql_close(&pconn);
        return false;
    }

    strcpy(Name,user->name);
    Name_len=strlen(Name);
    strcpy(Encrypted,user->encrypted);
    Encrypted_len=strlen(Encrypted);
    Pwd=0;
    Pwd_len=sizeof(int);

    ret=mysql_stmt_execute(stmt);
    if(ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(stmt),
               mysql_stmt_error(stmt));
        mysql_close(&pconn);
        return false;
    }

    return true;
}

int select_cryptpasswd(const char* username,MYSQL_STMT* stmt,MYSQL pconn,char* str){
    printf("select_cryptpasswd\n");

    char* sql="SELECT cryptpasswd FROM user_table WHERE username = ?";
    int count=prepare_count(stmt,&pconn,sql);

    char userName[20]={0};
    unsigned long Name_len=0;
    MYSQL_BIND bind;
    memset(&bind,0,sizeof(bind));

    bind.buffer_type=MYSQL_TYPE_VAR_STRING;
    bind.buffer=userName;
    bind.is_null=0;
    bind.length=&Name_len;

    int ret = mysql_stmt_bind_param(stmt, &bind);
    if(ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(stmt),
               mysql_stmt_error(stmt));
        mysql_close(&pconn);
        return false;
    }

    strcpy(userName,username);
    Name_len=strlen(userName);
    ret=mysql_stmt_execute(stmt);
    if(ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(stmt),
               mysql_stmt_error(stmt));
        mysql_close(&pconn);
        return false;
    }

    char str1[150];
    MYSQL_BIND res_bind;
    memset(&res_bind,0,sizeof(res_bind));
    res_bind.buffer_type=MYSQL_TYPE_VAR_STRING;
    res_bind.buffer=str1;
    res_bind.buffer_length=sizeof(str1);

    ret = mysql_stmt_bind_result(stmt, &res_bind);
    if(ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(stmt),
               mysql_stmt_error(stmt));
        mysql_close(&pconn);
    }

    mysql_stmt_fetch(stmt);
    strcpy(str,str1);
    return 1;

} 

int select_userid(const char* username,MYSQL_STMT* stmt,MYSQL pconn){
    printf("select_userid\n");

    char* sql="SELECT id FROM user_table WHERE username = ?";
    int count=prepare_count(stmt,&pconn,sql);

    char userName[20]={0};
    unsigned long Name_len=0;
    MYSQL_BIND bind;
    memset(&bind,0,sizeof(bind));

    bind.buffer_type=MYSQL_TYPE_VAR_STRING;
    bind.buffer=userName;
    bind.is_null=0;
    bind.length=&Name_len;

    int ret = mysql_stmt_bind_param(stmt, &bind);
    if(ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(stmt),
               mysql_stmt_error(stmt));
        mysql_close(&pconn);
        return false;
    }

    strcpy(userName,username);
    Name_len=strlen(userName);
    ret=mysql_stmt_execute(stmt);
    if(ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(stmt),
               mysql_stmt_error(stmt));
        mysql_close(&pconn);
    }

    int user_id;
    MYSQL_BIND res_bind;
    memset(&res_bind,0,sizeof(res_bind));
    res_bind.buffer_type=MYSQL_TYPE_LONG;
    res_bind.buffer=&user_id;
    res_bind.buffer_length=sizeof(user_id);

    ret=mysql_stmt_bind_result(stmt,&res_bind);
    mysql_stmt_fetch(stmt);
    return user_id;
}


int insert_filetable(int parent_id,char* filename,int id,MYSQL_STMT* stmt,MYSQL pconn){
    printf("insert_filetable\n");

    char* sql="INSERT INTO file_table(parent_id,filename,owner_id,md5,filesize,type,exist) VALUES(?,?,?,0,0,'d',1)";
    int count=prepare_count(stmt,&pconn,sql);

    int Parent_id;
    unsigned long Parentid_len=0;
    char Name[20]={0};
    unsigned long Name_len=0;
    int ID;
    unsigned long id_len=0;
    MYSQL_BIND bind[3];
    memset(bind,0,sizeof(bind));

    bind[0].buffer_type=MYSQL_TYPE_LONG;
    bind[0].buffer=&Parent_id;
    bind[0].is_null=0;
    bind[0].length=&Parentid_len;

    bind[1].buffer_type=MYSQL_TYPE_VAR_STRING;
    bind[1].buffer=Name;
    bind[1].is_null=0;
    bind[1].length=&Name_len;
 
    bind[2].buffer_type=MYSQL_TYPE_LONG;
    bind[2].buffer=&ID;
    bind[2].is_null=0;
    bind[2].length=&id_len;
    

    int ret = mysql_stmt_bind_param(stmt, bind);
    if(ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(stmt),
               mysql_stmt_error(stmt));
        mysql_close(&pconn);
        return false;
    }

    Parent_id=parent_id;
    Parentid_len=sizeof(int);
    strcpy(Name,filename);
    Name_len=strlen(Name);
    ID=id;
    id_len=sizeof(id);

    ret=mysql_stmt_execute(stmt);
    if(ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(stmt),
               mysql_stmt_error(stmt));
        mysql_close(&pconn);
        return false;
    }



    return ret;
}
int select_rootpwd(int user_id,MYSQL_STMT* stmt,MYSQL pconn){
    printf("select_rootpwd\n");


    char* sql="SELECT id FROM file_table WHERE owner_id=? AND parent_id=0";
    int count=prepare_count(stmt,&pconn,sql);

    int id;
    unsigned long id_len=0;
    MYSQL_BIND bind;
    memset(&bind,0,sizeof(bind));

    bind.buffer_type=MYSQL_TYPE_LONG;
    bind.buffer=&id;
    bind.is_null=0;
    bind.length=&id_len;

    int ret = mysql_stmt_bind_param(stmt, &bind);
    if(ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(stmt),
               mysql_stmt_error(stmt));
        mysql_close(&pconn);
        return false;
    }


    id=user_id;
    id_len=sizeof(int);
    ret=mysql_stmt_execute(stmt);
    if(ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(stmt),
               mysql_stmt_error(stmt));
        mysql_close(&pconn);
    }

    int ID;
    MYSQL_BIND res_bind;
    memset(&res_bind,0,sizeof(res_bind));
    res_bind.buffer_type=MYSQL_TYPE_LONG;
    res_bind.buffer=&ID;
    res_bind.buffer_length=sizeof(int);

    ret=mysql_stmt_bind_result(stmt,&res_bind);
    mysql_stmt_fetch(stmt);
    mysql_stmt_close(stmt);
    return ID;

}
int update_pwd(int user_id,int pwd,MYSQL_STMT* stmt,MYSQL pconn){
    printf("update_pwd\n");
    printf("user_id=%d \n",user_id);
    printf("pwd_id=%d\n",pwd);
    char* sql="UPDATE user_table set pwd=? WHERE id=?";
    int count=prepare_count(stmt,&pconn,sql);

    int id;
    unsigned long id_len=0;
    int PWD;
    unsigned long PWD_len=0;
    MYSQL_BIND bind[2];
    memset(bind,0,sizeof(bind));

    bind[0].buffer_type=MYSQL_TYPE_LONG;
    bind[0].buffer=&PWD;
    bind[0].is_null=0;
    bind[0].length=&PWD_len;

    bind[1].buffer_type=MYSQL_TYPE_LONG;
    bind[1].buffer=&id;
    bind[1].is_null=0;
    bind[1].length=&id_len;

    int ret = mysql_stmt_bind_param(stmt, bind);
    if(ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(stmt),
               mysql_stmt_error(stmt));
        mysql_close(&pconn);
        return false;
    }


    id=user_id;
    PWD=pwd;
    ret=mysql_stmt_execute(stmt);
    if(ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(stmt),
               mysql_stmt_error(stmt));
        mysql_close(&pconn);
    }

    return 1;

}

// 获取父目录ID的辅助函数
/* int getParentDirId(MYSQL *pconn, int user_id, int dir_id) */
/* { */
/*     // 实现获取父目录ID的逻辑 */
/*     const char *sql = "SELECT parent_id FROM file_table WHERE id = ? AND owner_id = ? AND exist = 1"; */
/*     MYSQL_STMT *stmt = mysql_stmt_init(pconn); */
/*     if (!stmt) */
/*     { */
/*         fprintf(stderr, "mysql_stmt_init() failed\n"); */
/*         return -1; */
/*     } */

/*     if (mysql_stmt_prepare(stmt, sql, strlen(sql))) */
/*     { */
/*         fprintf(stderr, "mysql_stmt_prepare() failed\n"); */
/*         fprintf(stderr, "%s\n", mysql_stmt_error(stmt)); */
/*         mysql_stmt_close(stmt); */
/*         return -1; */
/*     } */

/*     MYSQL_BIND bind[2]; */
/*     memset(bind, 0, sizeof(bind)); */

/*     bind[0].buffer_type = MYSQL_TYPE_LONG; */
/*     bind[0].buffer = (char *)&dir_id; */
/*     bind[0].is_null = 0; */
/*     bind[0].length = 0; */

/*     bind[1].buffer_type = MYSQL_TYPE_LONG; */
/*     bind[1].buffer = (char *)&user_id; */
/*     bind[1].is_null = 0; */
/*     bind[1].length = 0; */

/*     if (mysql_stmt_bind_param(stmt, bind)) */
/*     { */
/*         fprintf(stderr, "mysql_stmt_bind_param() failed\n"); */
/*         fprintf(stderr, "%s\n", mysql_stmt_error(stmt)); */
/*         mysql_stmt_close(stmt); */
/*         return -1; */
/*     } */

/*     if (mysql_stmt_execute(stmt)) */
/*     { */
/*         fprintf(stderr, "mysql_stmt_execute() failed\n"); */
/*         fprintf(stderr, "%s\n", mysql_stmt_error(stmt)); */
/*         mysql_stmt_close(stmt); */
/*         return -1; */
/*     } */

/*     int parent_id; */
/*     MYSQL_BIND result_bind[1]; */
/*     memset(result_bind, 0, sizeof(result_bind)); */

/*     result_bind[0].buffer_type = MYSQL_TYPE_LONG; */
/*     result_bind[0].buffer = (char *)&parent_id; */
/*     result_bind[0].is_null = 0; */
/*     result_bind[0].length = 0; */

/*     if (mysql_stmt_bind_result(stmt, result_bind)) */
/*     { */
/*         fprintf(stderr, "mysql_stmt_bind_result() failed\n"); */
/*         fprintf(stderr, "%s\n", mysql_stmt_error(stmt)); */
/*         mysql_stmt_close(stmt); */
/*         return -1; */
/*     } */

/*     if (mysql_stmt_fetch(stmt) != 0) */
/*     { */
/*         parent_id = -1; */
/*     } */

/*     mysql_stmt_close(stmt); */
/*     return parent_id; */
/* } */

/* int updateUserPwd(MYSQL *pconn, int user_id, int pwd) */
/* { */
/*     MYSQL_STMT *stmt = mysql_stmt_init(pconn); */
/*     if (!stmt) */
/*     { */
/*         fprintf(stderr, "mysql_stmt_init() failed\n"); */
/*         return -1; */
/*     } */

/*     const char *sql = "UPDATE user_table SET pwd = ? WHERE id = ?"; */

/*     if (mysql_stmt_prepare(stmt, sql, strlen(sql))) */
/*     { */
/*         fprintf(stderr, "mysql_stmt_prepare() failed\n"); */
/*         fprintf(stderr, "%s\n", mysql_stmt_error(stmt)); */
/*         mysql_stmt_close(stmt); */
/*         return -1; */
/*     } */

/*     MYSQL_BIND bind[2]; */
/*     memset(bind, 0, sizeof(bind)); */

/*     bind[0].buffer_type = MYSQL_TYPE_LONG; */
/*     bind[0].buffer = (char *)&pwd; */
/*     bind[0].is_null = 0; */
/*     bind[0].length = 0; */

/*     bind[1].buffer_type = MYSQL_TYPE_LONG; */
/*     bind[1].buffer = (char *)&user_id; */
/*     bind[1].is_null = 0; */
/*     bind[1].length = 0; */

/*     if (mysql_stmt_bind_param(stmt, bind)) */
/*     { */
/*         fprintf(stderr, "mysql_stmt_bind_param() failed\n"); */
/*         fprintf(stderr, "%s\n", mysql_stmt_error(stmt)); */
/*         mysql_stmt_close(stmt); */
/*         return -1; */
/*     } */

/*     if (mysql_stmt_execute(stmt)) */
/*     { */
/*         fprintf(stderr, "mysql_stmt_execute() failed\n"); */
/*         fprintf(stderr, "%s\n", mysql_stmt_error(stmt)); */
/*         mysql_stmt_close(stmt); */
/*         return -1; */
/*     } */

/*     mysql_stmt_close(stmt); */
/*     return 0; */
/* } */
/* int getDirId(MYSQL *pconn, int user_id, int parent_id, const char *dir_name) */
/* { */
/*     MYSQL_STMT *stmt = mysql_stmt_init(pconn); */
/*     if (!stmt) */
/*     { */
/*         fprintf(stderr, "mysql_stmt_init() failed\n"); */
/*         return -1; */
/*     } */

/*     const char *sql = "SELECT id FROM file_table WHERE owner_id = ? AND parent_id = ? AND filename = ? AND exist = 1 AND type = 'd'"; */

/*     if (mysql_stmt_prepare(stmt, sql, strlen(sql))) */
/*     { */
/*         fprintf(stderr, "mysql_stmt_prepare() failed\n"); */
/*         fprintf(stderr, "%s\n", mysql_stmt_error(stmt)); */
/*         mysql_stmt_close(stmt); */
/*         return -1; */
/*     } */

/*     MYSQL_BIND bind[3]; */
/*     memset(bind, 0, sizeof(bind)); */

/*     bind[0].buffer_type = MYSQL_TYPE_LONG; */
/*     bind[0].buffer = (char *)&user_id; */
/*     bind[0].is_null = 0; */
/*     bind[0].length = 0; */

/*     bind[1].buffer_type = MYSQL_TYPE_LONG; */
/*     bind[1].buffer = (char *)&parent_id; */
/*     bind[1].is_null = 0; */
/*     bind[1].length = 0; */

/*     bind[2].buffer_type = MYSQL_TYPE_STRING; */
/*     bind[2].buffer = (char *)dir_name; */
/*     bind[2].buffer_length = strlen(dir_name); */
/*     bind[2].is_null = 0; */
/*     bind[2].length = 0; */

/*     if (mysql_stmt_bind_param(stmt, bind)) */
/*     { */
/*         fprintf(stderr, "mysql_stmt_bind_param() failed\n"); */
/*         fprintf(stderr, "%s\n", mysql_stmt_error(stmt)); */
/*         mysql_stmt_close(stmt); */
/*         return -1; */
/*     } */

/*     if (mysql_stmt_execute(stmt)) */
/*     { */
/*         fprintf(stderr, "mysql_stmt_execute() failed\n"); */
/*         fprintf(stderr, "%s\n", mysql_stmt_error(stmt)); */
/*         mysql_stmt_close(stmt); */
/*         return -1; */
/*     } */

/*     MYSQL_BIND result_bind[1]; */
/*     memset(result_bind, 0, sizeof(result_bind)); */

/*     int dir_id; */
/*     result_bind[0].buffer_type = MYSQL_TYPE_LONG; */
/*     result_bind[0].buffer = (char *)&dir_id; */
/*     result_bind[0].is_null = 0; */
/*     result_bind[0].length = 0; */

/*     if (mysql_stmt_bind_result(stmt, result_bind)) */
/*     { */
/*         fprintf(stderr, "mysql_stmt_bind_result() failed\n"); */
/*         fprintf(stderr, "%s\n", mysql_stmt_error(stmt)); */
/*         mysql_stmt_close(stmt); */
/*         return -1; */
/*     } */

/*     if (mysql_stmt_fetch(stmt) == 0) */
/*     { */
/*         mysql_stmt_close(stmt); */
/*         return dir_id; */
/*     } */
/*     else */
/*     { */
/*         mysql_stmt_close(stmt); */
/*         return -1; */
/*     } */
/* } */


//mcx:puts&gets------------------------------------------------------------------------------------
bool if_md5_exist(char md5_result[],int n,int *filesize,MYSQL_STMT *stmt,MYSQL *conn)
{
    bool exists = false;
    int result_filesize;
    MYSQL_BIND bind[2];
        // 准备查询语句
    const char *query = "SELECT filesize FROM file_table WHERE md5 = ?";
    if (mysql_stmt_prepare(stmt, query, strlen(query)) != 0) {
        fprintf(stderr, "mysql_stmt_prepare() failed. Error: %s\n", mysql_stmt_error(stmt));
        return false;
    }

    // 绑定参数
    memset(bind, 0, sizeof(bind));

    // 绑定输入参数
    bind[0].buffer_type = MYSQL_TYPE_STRING;
    bind[0].buffer = (char *)md5_result;
    bind[0].buffer_length = strlen(md5_result);

    if (mysql_stmt_bind_param(stmt, bind) != 0) {
        fprintf(stderr, "mysql_stmt_bind_param() failed. Error: %s\n", mysql_stmt_error(stmt));
        return false;
    }

    // 执行查询
    if (mysql_stmt_execute(stmt) != 0) {
        fprintf(stderr, "mysql_stmt_execute() failed. Error: %s\n", mysql_stmt_error(stmt));
        return false;
    }

    // 绑定输出参数
    memset(bind, 0, sizeof(bind));

    bind[0].buffer_type = MYSQL_TYPE_LONG;
    bind[0].buffer = (char *)&result_filesize;
    bind[0].buffer_length = sizeof(result_filesize);

    if (mysql_stmt_bind_result(stmt, bind) != 0) {
        fprintf(stderr, "mysql_stmt_bind_result() failed. Error: %s\n", mysql_stmt_error(stmt));
        return false;
    }

    // 获取结果
    if (mysql_stmt_fetch(stmt) == 0) {
        exists = true;
        *filesize = result_filesize;
    }


    return exists;

}

bool check_filename_exists_with_pwd( char *filename, int pwd,MYSQL *conn)
{
    MYSQL_RES *res;
    MYSQL_ROW row;
    char query[512];
    bool exists = false;

    // 准备查询语句
    snprintf(query, sizeof(query),
             "SELECT 1 FROM file_table f JOIN user_table u ON f.owner_id = u.id WHERE f.filename = '%s' AND f.parent_id = %d and f.type = 'f'",
             filename, pwd);
    // 执行查询
    if (mysql_query(conn, query)) {
        fprintf(stderr, "SELECT query failed. Error: %s\n", mysql_error(conn));
        return false;
    }
    // 获取结果
    res = mysql_store_result(conn);
    if (res == NULL) {
        fprintf(stderr, "mysql_store_result() failed. Error: %s\n", mysql_error(conn));
        return false;
    }

    // 检查是否有结果行
    if ((row = mysql_fetch_row(res)) != NULL) {
        exists = true;
    }
    return exists;

}




/*
bool insert_puts_file(MYSQL *conn,int pwd,char * filename,int user_id,char md5_result[],int n,int filesize)
{
    char query[512];
    bool result = false;

    // 准备插入语句
    snprintf(query, sizeof(query), "INSERT INTO file_table (parent_id, filename, owner_id, md5, filesize, type, exist) VALUES (%d, '%s', %d, '%s', %d, 'f', 1)",
             pwd, filename, user_id, md5_result, filesize);

    // 执行插入
    if (mysql_query(conn, query) == 0) {
        result = true;
    } else {
        fprintf(stderr, "INSERT query failed. Error: %s\n", mysql_error(conn));
    }

    return result;

}
*/

bool insert_puts_file(MYSQL *conn, MYSQL_STMT *stmt, int parent_id, const char *filename, int owner_id, const char md5_result[],int n, int filesize) {
    bool result = false;
    MYSQL_BIND bind[6];


    // 准备预处理语句
    const char *query = "INSERT INTO file_table (parent_id, filename, owner_id, md5, filesize, type, exist) VALUES (?, ?, ?, ?, ?, 'f', 1)";
    if (mysql_stmt_prepare(stmt, query, strlen(query))) {
        fprintf(stderr, "mysql_stmt_prepare() failed. Error: %s\n", mysql_stmt_error(stmt));
        return false;
    }

    // 清空绑定结构体
    memset(bind, 0, sizeof(bind));

    // 绑定参数
    bind[0].buffer_type = MYSQL_TYPE_LONG;
    bind[0].buffer = (char *)&parent_id;
    bind[0].is_null = 0;
    bind[0].length = 0;

    bind[1].buffer_type = MYSQL_TYPE_STRING;
    bind[1].buffer = (char *)filename;
    bind[1].buffer_length = strlen(filename);
    bind[1].is_null = 0;
    bind[1].length = 0;

    bind[2].buffer_type = MYSQL_TYPE_LONG;
    bind[2].buffer = (char *)&owner_id;
    bind[2].is_null = 0;
    bind[2].length = 0;

    bind[3].buffer_type = MYSQL_TYPE_STRING;
    bind[3].buffer = (char *)md5_result;
    bind[3].buffer_length = strlen(md5_result);
    bind[3].is_null = 0;
    bind[3].length = 0;

    bind[4].buffer_type = MYSQL_TYPE_LONG;
    bind[4].buffer = (char *)&filesize;
    bind[4].is_null = 0;
    bind[4].length = 0;

    // 绑定参数到语句
    if (mysql_stmt_bind_param(stmt, bind)) {
        fprintf(stderr, "mysql_stmt_bind_param() failed. Error: %s\n", mysql_stmt_error(stmt));
        return false;
    }

    // 执行预处理语句
    if (mysql_stmt_execute(stmt) == 0) {
        result = true;
    } else {
        fprintf(stderr, "INSERT query failed. Error: %s\n", mysql_stmt_error(stmt));
    }

    // 重置语句准备再次使用
    mysql_stmt_reset(stmt);

    return result;
}

bool get_md5_by_filename(int pwd,char *filename,char* md5_buffer,MYSQL_STMT *stmt,MYSQL *conn)
{
    MYSQL_BIND bind[2];
    MYSQL_BIND result[1];
    unsigned long length;
    char md5[50];
        // 准备查询语句
    const char *query = "SELECT f.md5 FROM file_table f JOIN user_table u ON f.owner_id = u.id WHERE f.filename = ? AND u.pwd = ? and f.type = 'f'";
    if (mysql_stmt_prepare(stmt, query, strlen(query)) != 0) {
        fprintf(stderr, "mysql_stmt_prepare() failed. Error: %s\n", mysql_stmt_error(stmt));
        return false;
    }

        // 绑定参数
    memset(bind, 0, sizeof(bind));

    bind[0].buffer_type = MYSQL_TYPE_STRING;
    bind[0].buffer = (char *)filename;
    bind[0].buffer_length = strlen(filename);

    bind[1].buffer_type = MYSQL_TYPE_LONG;
    bind[1].buffer = (char *)&pwd;
    bind[1].buffer_length = sizeof(pwd);

    if (mysql_stmt_bind_param(stmt, bind) != 0) {
        fprintf(stderr, "mysql_stmt_bind_param() failed. Error: %s\n", mysql_stmt_error(stmt));
        return false;
    }

    // 执行查询
    if (mysql_stmt_execute(stmt) != 0) {
        fprintf(stderr, "mysql_stmt_execute() failed. Error: %s\n", mysql_stmt_error(stmt));
        return false;
    }

        // 绑定输出参数
    memset(result, 0, sizeof(result));

    result[0].buffer_type = MYSQL_TYPE_STRING;
    result[0].buffer = (char *)md5;
    result[0].buffer_length = sizeof(md5);
    result[0].length = &length;

    if (mysql_stmt_bind_result(stmt, result) != 0) {
        fprintf(stderr, "mysql_stmt_bind_result() failed. Error: %s\n", mysql_stmt_error(stmt));
        return false;
    }

        // 获取结果
    if (mysql_stmt_fetch(stmt) == 0) {
        strncpy(md5_buffer, md5, length);
        md5_buffer[length] = '\0'; // 确保字符串以null结尾
        return true;
    } else {
        fprintf(stderr, "No result found for filename: %s\n", filename);
        return false;
    }

}
//MCX:END:for puts&gets----------------------------------------------------------------------------------------------------


int getDirid(MYSQL *pconn, int user_id, int parent_id, const char *dir_name)
{
    MYSQL_STMT *stmt = mysql_stmt_init(pconn);
    if (!stmt)
    {
        fprintf(stderr, "mysql_stmt_init() failed\n");
        return -1;
    }

    const char *sql = "SELECT id FROM file_table WHERE owner_id = ? AND parent_id = ? AND filename = ? AND exist = 1 AND type = 'd'";

    if (mysql_stmt_prepare(stmt, sql, strlen(sql)))
    {
        fprintf(stderr, "mysql_stmt_prepare() failed\n");
        fprintf(stderr, "%s\n", mysql_stmt_error(stmt));
        mysql_stmt_close(stmt);
        return -1;
    }

    MYSQL_BIND bind[3];
    memset(bind, 0, sizeof(bind));

    bind[0].buffer_type = MYSQL_TYPE_LONG;
    bind[0].buffer = (char *)&user_id;
    bind[0].is_null = 0;
    bind[0].length = 0;

    bind[1].buffer_type = MYSQL_TYPE_LONG;
    bind[1].buffer = (char *)&parent_id;
    bind[1].is_null = 0;
    bind[1].length = 0;

    bind[2].buffer_type = MYSQL_TYPE_STRING;
    bind[2].buffer = (char *)dir_name;
    bind[2].buffer_length = strlen(dir_name);
    bind[2].is_null = 0;
    bind[2].length = 0;

    if (mysql_stmt_bind_param(stmt, bind))
    {
        fprintf(stderr, "mysql_stmt_bind_param() failed\n");
        fprintf(stderr, "%s\n", mysql_stmt_error(stmt));
        mysql_stmt_close(stmt);
        return -1;
    }

    if (mysql_stmt_execute(stmt))
    {
        fprintf(stderr, "mysql_stmt_execute() failed\n");
        fprintf(stderr, "%s\n", mysql_stmt_error(stmt));
        mysql_stmt_close(stmt);
        return -1;
    }

    MYSQL_BIND result_bind[1];
    memset(result_bind, 0, sizeof(result_bind));

    int dir_id;
    result_bind[0].buffer_type = MYSQL_TYPE_LONG;
    result_bind[0].buffer = (char *)&dir_id;
    result_bind[0].is_null = 0;
    result_bind[0].length = 0;

    if (mysql_stmt_bind_result(stmt, result_bind))
    {
        fprintf(stderr, "mysql_stmt_bind_result() failed\n");
        fprintf(stderr, "%s\n", mysql_stmt_error(stmt));
        mysql_stmt_close(stmt);
        return -1;
    }

    if (mysql_stmt_fetch(stmt) == 0)
    {
        mysql_stmt_close(stmt);
        return dir_id;
    }
    else
    {
        mysql_stmt_close(stmt);
        return -1;
    }
}
int updateExit(MYSQL pconn,MYSQL_STMT* stmt,int id){
    printf("updateExit\n");

    char* sql="UPDATE file_table set exist = 0 where id=?";
    int count = prepare_count(stmt,&pconn,sql);

    if (count < 0) {
        printf("Failed to prepare statement.\n");
        return false;
    }

    int ID;
    unsigned long ID_len=0;

     MYSQL_BIND bind;
     memset(&bind,0,sizeof(bind));

     bind.buffer_type = MYSQL_TYPE_LONG;
     bind.buffer=&ID;
     bind.is_null=0;
     bind.length=&ID_len;


      int ret = mysql_stmt_bind_param(stmt, &bind);
      if(ret) {
          printf("(%d, %s)\n", mysql_stmt_errno(stmt),
                 mysql_stmt_error(stmt));
          return -1;
          }

     ID=id;
     ID_len=sizeof(int);

     ret=mysql_stmt_execute(stmt);
     if(ret) {
     printf("(%d, %s)\n", mysql_stmt_errno(stmt),
           mysql_stmt_error(stmt));
     return -1;
}
 return 0;

}

